﻿/*----------------------------------------------------------------
//  <copyright file="ExpirableBase.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-12-06 18:05</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.Entity
{
    #region "可过期实体基类"
    /// <summary>
    /// 可过期实体基类
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    public abstract class ExpirableBase<TKey> : EntityBase<TKey>, IExpirable
        where TKey : IEquatable<TKey>
    {
        #region "属性"
        #region "获取或设置 生效时间"
        /// <summary>
        /// 获取或设置 生效时间
        /// </summary>
        [Column("BeginTime")]
        [DisplayName("生效时间")]
        public DateTime? BeginTime { get; set; }
        #endregion
        #region "获取或设置 过期时间"
        /// <summary>
        /// 获取或设置 过期时间
        /// </summary>
        [Column("EndTime")]
        [DisplayName("过期时间")]
        public DateTime? EndTime { get; set; }
        #endregion

        #endregion

        #region "方法"
        #region "验证时间生效时间与过期时间是否有效"
        /// <summary>
        /// 验证时间生效时间与过期时间是否有效
        /// </summary>
        /// <returns></returns>
        public bool IsTimeValid()
        {
            return !BeginTime.HasValue || !EndTime.HasValue || BeginTime.Value <= EndTime.Value;
        }
        #endregion
        #region "验证时间有效性，无效则抛出异常"
        /// <summary>
        /// 验证时间有效性，无效则抛出异常
        /// </summary>
        public void ThrowIfTimeInvalid()
        {
            if (IsTimeValid())
            {
                return;
            }
            throw new IndexOutOfRangeException(I18N.T("生效时间不能大于过期时间"));
        }
        #endregion

        #endregion

    }
    #endregion

}
